package designPattern;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy {
    public static void main(String[] args) {
        Project2 realProjcet = new JdkProxyHandler<Project2>(new RealProject2()).getProxyInstance();
        realProjcet.development();
    }
}

interface Project {
    void development();
}

class RealProject implements Project {
    @Override
    public void development() {
        System.out.println("development project with Java");
    }
}

class JdkProxyHandler<C> {
    private C proxyClass;

    public JdkProxyHandler(C proxyClass) {
        this.proxyClass = proxyClass;
    }

    public C getProxyInstance() {
        return (C)Proxy.newProxyInstance(proxyClass.getClass().getClassLoader(), proxyClass.getClass().getInterfaces(), new RealHandler(proxyClass));
    }

    static class RealHandler implements InvocationHandler {
        private Object proxyClass;

        public RealHandler(Object proxyClass) {
            this.proxyClass = proxyClass;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("before doing");
            Object object = method.invoke(proxyClass, args);
            System.out.println("after doing");
            return object;
        }
    }

}